//
//  GQHOrderModel.m
//  Xanthium
//
//  Created by GuanQinghao on 23/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHOrderModel.h"
#import "GQHHeader.h"
#import "GQHContractPaymentModel.h"

@implementation GQHOrderModel

+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper {
    
    return @{
             @"qh_orderID" : @"id",
             @"qh_orderNumber" : @"waybillAwb",
             @"qh_driverLoadedTime" : @"confirmLoadTime",
             @"qh_driverUnloadedTime" : @"confirmUnloadTime",
             @"qh_driverContractTime" : @"affProTime",
             @"qh_ownerUnloadedTime" : @"shiComTime",
             @"qh_cancelTime" : @"cancelTime",
             @"qh_orderState" : @"waybillState",
             @"qh_orderReviewState" : @"evaluateState",
             @"qh_orderReviewedState" : @"reviewedState",
             @"qh_invoiceState" : @"invoiceStatus",
             @"qh_attachment" : @"attachment",
             @"qh_largeTextID" : @"largeTextId",
             
             @"qh_contractID" : @"waybillAgId",
             @"qh_qutationID" : @"quotationId",
             @"qh_mileage" : @"mileage",
             @"qh_freightWeight" : @"demandVehicle",
             @"qh_consignee" : @"consignee",
             @"qh_consigneeMobile" : @"consigneePhone",
             @"qh_consigningTime" : @"goodsTime",
             
             @"qh_contractPrepaymentSum" : @"freightPrepaidSum",
             @"qh_contractFinalPaymentSum" : @"freifhtTailedSum",
             @"qh_contractOtherExpenseSum" : @"otherExpensesSum",
             @"qh_amount" : @"amount",
             
             @"qh_freightID" : @"goodsSupplyId",
             @"qh_departureID" : @"startPlace",
             @"qh_departure" : @"startPlace",
             @"qh_departureLatitude" : @"originLatitude",
             @"qh_departureLongitude" : @"originLongitude",
             @"qh_destinationLatitude" : @"destinationLatitude",
             @"qh_destinationLongitude" : @"destinationLongitude",
             @"qh_destinationID" : @"endPlace",
             @"qh_destination" : @"endPlace",
             @"qh_modelWanted" : @"demandModels",
             @"qh_lengthWanted" : @"demandVehicleLength",
             @"qh_loadingTime" : @"loadingTime",
             @"qh_freightType" : @"goodsType",
             @"qh_freightTypeCode" : @"goodsType",
             
             @"qh_driverID" : @"driverId",
             @"qh_driverName" : @"driveName",
             @"qh_driverAvatar" : @"driverHeadImage",
             @"qh_driverMobile" : @"driverPhone",
             @"qh_driverScore" : @"driveScore",
             @"qh_modelCode" : @"vehicleclassificationcode",
             @"qh_model" : @"vehicleclassificationcode",
             @"qh_normalLength" : @"carLength",
             @"qh_normalLoading" : @"carLoad",
             @"qh_licensePlateNumber" : @"licPlateNum",
             
             @"qh_ownerID" : @"ownerId",
             @"qh_ownerAvatar" : @"ownerHeadImage",
             @"qh_ownerName" : @"ownerName",
             @"qh_ownerMobile" : @"ownerPhone",
             @"qh_ownerScore" : @"ownerScore",
             @"qh_ownerType" : @"ownerType",
             
             @"qh_contractPrepayment" : @"freightPrepaid",
             @"qh_contractFinalPayment" : @"freightTailed",
             @"qh_contractOtherExpense" : @"otherExpenses"
             
             };
}

+ (nullable NSDictionary<NSString *, id> *)modelContainerPropertyGenericClass {
    
    return @{
             @"qh_contractPrepayment" : GQHContractPaymentModel.class,
             @"qh_contractFinalPayment" : GQHContractPaymentModel.class,
             @"qh_contractOtherExpense" : GQHContractPaymentModel.class
             };
}

///  获取订单列表
+ (void)qh_fetchOrdersWithParameters:(NSDictionary *)parameters success:(void(^)(BOOL status, NSNumber *code, NSString *message, NSArray<GQHOrderModel *> *orders))success failure:(void(^)(NSError *error))failure {
    
    
    NSMutableString *URLString = [NSMutableString string];
    
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        URLString = [NSMutableString stringWithFormat:@"%@%@",[NSString qh_APIURLString],@"/buWaybill/1/findAllDriver"];
    } else {
        
        URLString = [NSMutableString stringWithFormat:@"%@%@",[NSString qh_APIURLString],@"/buWaybill/1/findAllOwner"];
    }
    
    [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        DLog(@"%@---%@---%@",code,message,data);
        
        if (0 == [code integerValue]) {
            
            NSMutableArray *orderArray = [NSMutableArray array];
            for (NSDictionary *object in [data objectForKey:@"list"]) {
                
                GQHOrderModel *orderModel = [GQHOrderModel yy_modelWithDictionary:object];
                if (orderModel) {
                    
                    [orderArray addObject:orderModel];
                }
            }
            success(YES,code,message,orderArray);
        } else {
            
            success(NO,code,message,nil);
        }
    } failure:^(NSError *error) {
        
        failure(error);
    }];
}

///  获取订单详情
+ (void)qh_fetchOrderDetailWithParameters:(NSDictionary *)parameters success:(void(^)(BOOL status, NSNumber *code, NSString *message, GQHOrderModel *order))success failure:(void(^)(NSError *error))failure {
    
    NSString *URLString = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"/buWaybill/1/findOne"];
    [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        DLog(@"%@---%@---%@",code,message,data);
        
        if (0 == [code integerValue]) {
            
            GQHOrderModel *orderModel = [GQHOrderModel yy_modelWithDictionary:data];
            success(YES,code,message,orderModel);
        } else {
            
            success(NO,code,message,nil);
        }
    } failure:^(NSError *error) {
        
        failure(error);
    }];
}

@end
